package com.zhuliwei.rabbitmq.direct.consumer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhuliwei.domain.entity.Comment;
import com.zhuliwei.domain.entity.Notice;
import com.zhuliwei.domain.entity.User;
import com.zhuliwei.mapper.NoticeMapper;
import com.zhuliwei.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.zhuliwei.constants.RabbitConstants.NOTICE_QUEUE_NAME;

/**
 * Notice评论消息消费
 *
 * @author 朱力维
 * @time 2024/11/14 15:15
 **/
@Component
@RabbitListener(queues = NOTICE_QUEUE_NAME)
public class NoticeCommentConsumer {
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private UserMapper userMapper;

    @RabbitHandler
    public void addCommentNotice(Comment comment) {
        Notice notice = new Notice();
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.select(User::getNickName);
        lqw.eq(User::getId, comment.getCreateBy());
        User user = userMapper.selectOne(lqw);
        notice.setContent("您收到一条来自\"" + user.getNickName() + "\"的评论回复：\"" + comment.getContent() + "\"");
        notice.setArticleId(comment.getArticleId());
        notice.setType("0");
        notice.setUserId(comment.getToCommentUserId());
        notice.setCreateBy(comment.getCreateBy());
        notice.setCreateTime(new Date());
        noticeMapper.insert(notice);
    }
}
